const ScoreModel = require('../models/ScoreModel')

const ScoreService = {
  findList: async ({ courseid }, body) => {
    return ScoreModel.find({ courseid, username: body.username })
  },

  postList: async ({ courseid }, body) => {
    return ScoreModel.create({
      username: body.username,
      courseid: courseid,
      daily: body.daily,
      exp: body.exp,
      mid: body.mid,
      final: body.final,
      ispublish: body.ispublish,
      ispass: body.ispass,
      reexam: body.reexam
    })
  },

  putList: async ({ courseid }, body) => {
    if (body.reexam) {
      return ScoreModel.updateOne(
        {
          courseid: courseid,
          username: body.username
        },
        {
          daily: body.daily,
          exp: body.exp,
          mid: body.mid,
          final: body.final,
          ispublish: body.ispublish,
          ispass: body.ispass,
          reexam: body.reexam
        }
      )
    } else {
      return ScoreModel.updateOne(
        {
          courseid: courseid,
          username: body.username
        },
        {
          daily: body.daily,
          exp: body.exp,
          mid: body.mid,
          final: body.final,
          ispublish: body.ispublish,
          ispass: body.ispass,
          reexam: body.reexam
        }
      )
    }
  },

  getList: async ({ courseid }) => {
    return ScoreModel.find({ courseid })
  },

  getNopassList: async ({ courseid }) => {
    return ScoreModel.find({ courseid, ispass: 0 })
  },

  getPublishedList: async ({ username }) => {
    return ScoreModel.find({ username, ispublish: 1 })
  }
}

module.exports = ScoreService
